package com.chartdemo.view;

import java.util.ArrayList;
import java.util.List;

import org.achartengine.GraphicalView;
import org.achartengine.chart.BarChart;
import org.achartengine.chart.BubbleChart;
import org.achartengine.chart.CombinedXYChart;
import org.achartengine.chart.CubicLineChart;
import org.achartengine.chart.LineChart;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.model.XYValueSeries;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Rect;
import android.graphics.RectF;
import android.view.View;

/**
 * 绘制图标的视图 1.绘制X和Y线轴框 a.顶部设置title b.底部设置图标识 c.x,y轴标识
 * 
 * @author huangtg
 */
public class ColumnChartView extends View
{
	/**
	 * 矩阵
	 */
	private Rect mRect = new Rect();
	
	/**
	 * 图表属性
	 */
	private XYMultipleSeriesRenderer renderer;
	
	/**
	 * 图表
	 */
	private CombinedXYChart mChart;
	
	/**
	 * 样式
	 */
	private String[] types;
	
	/** The zoom buttons background color. */
	private static final int ZOOM_BUTTONS_COLOR = Color.argb(175, 150, 150, 150);
	
	/** The zoom area size. */
	private int zoomSize = 50;
	
	/** The zoom buttons rectangle. */
	private RectF mZoomR = new RectF();
	
	/**
	 * 画笔
	 */
	private Paint mPaint = new Paint();
	
	/**
	 * scan smaller
	 */
	private Bitmap zoomInImage;
	
	/**
	 * scan bigger
	 */
	private Bitmap zoomOutImage;
	
	/**
	 * scan fit
	 */
	private Bitmap fitZoomImage;
	
	private XYSeries waterSeries;
	
	private double[] mData;
	
	private Context mContext;
	
	private int[] mColors;
	
	private PointStyle[] mStyles;
	
	public ColumnChartView(Context context, int[] colors, PointStyle[] styles, double[] data)
	{
		super(context);
		mData = data;
		mContext = context;
		mColors = colors;
		mStyles = styles;
		this.init(mContext, mColors, mStyles, mData);
	}
	
	@Override
	protected void onDraw(Canvas canvas)
	{
		super.onDraw(canvas);
		canvas.getClipBounds(mRect);
		int top = mRect.top;
		int left = mRect.left;
		int width = mRect.width();
		int height = mRect.height();
		if (true)
		{
			top = 0;
			left = 0;
			width = getMeasuredWidth();
			height = getMeasuredHeight();
		}
		mChart.draw(canvas, left, top, width, height, mPaint);
		if (renderer != null && renderer.isZoomEnabled() && renderer.isZoomButtonsVisible())
		{
			mPaint.setColor(ZOOM_BUTTONS_COLOR);
			zoomSize = Math.max(zoomSize, Math.min(width, height) / 7);
			mZoomR.set(left + width - zoomSize * 3, top + height - zoomSize * 0.775f, left + width, top
					+ height);
			canvas.drawRoundRect(mZoomR, zoomSize / 3, zoomSize / 3, mPaint);
			float buttonY = top + height - zoomSize * 0.625f;
			canvas.drawBitmap(zoomInImage, left + width - zoomSize * 2.75f, buttonY, null);
			canvas.drawBitmap(zoomOutImage, left + width - zoomSize * 1.75f, buttonY, null);
			canvas.drawBitmap(fitZoomImage, left + width - zoomSize * 0.75f, buttonY, null);
		}
	}
	
	/**
	 * @param colors 画笔颜色
	 * @param styles 图形样式
	 * @return XYMultipleSeriesRenderer
	 */
	private XYMultipleSeriesRenderer buildRenderer(int[] colors, PointStyle[] styles)
	{
		XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
		setRenderer(renderer, colors, styles);
		return renderer;
	}
	
	private void setRenderer(XYMultipleSeriesRenderer renderer, int[] colors, PointStyle[] styles)
	{
		renderer.setAxisTitleTextSize(16);
		renderer.setChartTitleTextSize(20);
		renderer.setLabelsTextSize(15);
		renderer.setLegendTextSize(15);
		renderer.setPointSize(5f);
		renderer.setMargins(new int[] { 20, 30, 15, 20 });
		int length = colors.length;
		for (int i = 0; i < length; i++)
		{
			XYSeriesRenderer r = new XYSeriesRenderer();
			r.setColor(colors[i]);
			r.setPointStyle(styles[i]);
			renderer.addSeriesRenderer(r);
		}
	}
	
	/**
	 * @param titles 图形标识
	 * @param xValues x轴值
	 * @param yValues yz轴值
	 * @return XYMultipleSeriesDataset
	 */
	protected XYMultipleSeriesDataset buildDataset(String[] titles, List<double[]> xValues,
			List<double[]> yValues)
	{
		XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
		addXYSeries(dataset, titles, xValues, yValues, 0);
		return dataset;
	}
	
	public void addXYSeries(XYMultipleSeriesDataset dataset, String[] titles, List<double[]> xValues,
			List<double[]> yValues, int scale)
	{
		int length = titles.length;
		for (int i = 0; i < length; i++)
		{
			XYSeries series = new XYSeries(titles[i], scale);
			double[] xV = xValues.get(i);
			double[] yV = yValues.get(i);
			int seriesLength = xV.length;
			for (int k = 0; k < seriesLength; k++)
			{
				series.add(xV[k], yV[k]);
			}
			dataset.addSeries(series);
		}
	}
	
	/**
	 * @param context 上下文
	 * @param colors 画笔颜色
	 * @param styles 图形样式
	 */
	private void init(Context context, int[] colors, PointStyle[] styles, double[] mData)
	{
		zoomInImage = BitmapFactory
				.decodeStream(GraphicalView.class.getResourceAsStream("image/zoom_in.png"));
		zoomOutImage = BitmapFactory.decodeStream(GraphicalView.class
				.getResourceAsStream("image/zoom_out.png"));
		fitZoomImage = BitmapFactory
				.decodeStream(GraphicalView.class.getResourceAsStream("image/zoom-1.png"));
		types = new String[] { BarChart.TYPE, BubbleChart.TYPE, LineChart.TYPE, CubicLineChart.TYPE };
		
		String[] titles = new String[] { "Crete空气温度", "Skiathos空气温度" };
		List<double[]> x = new ArrayList<double[]>();
		for (int i = 0; i < titles.length; i++)
		{
			x.add(new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 });
		}
		List<double[]> values = new ArrayList<double[]>();
		values.add(new double[] { 12.3, 12.5, 13.8, 16.8, 20.4, 24.4, 26.4, 26.1, 23.6, 20.3, 17.2, 13.9 });
		values.add(new double[] { 9, 10, 11, 15, 19, 23, 26, 25, 22, 18, 13, 10 });
		
		renderer = buildRenderer(colors, styles);
		renderer.setPointSize(5.5f);
		int mLength = renderer.getSeriesRendererCount();
		
		for (int i = 0; i < mLength; i++)
		{
			XYSeriesRenderer r = (XYSeriesRenderer) renderer.getSeriesRendererAt(i);
			r.setLineWidth(5);
			r.setFillPoints(true);
		}
		setChartSettings(renderer, "Weather data", "Month", "Temperature", 0.5, 12.5, 0, 40, Color.LTGRAY,
				Color.LTGRAY);
		
		renderer.setXLabels(12);
		renderer.setYLabels(10);
		renderer.setShowGrid(true);
		renderer.setXLabelsAlign(Align.RIGHT);
		renderer.setYLabelsAlign(Align.RIGHT);
		renderer.setZoomButtonsVisible(true);
		renderer.setPanLimits(new double[] { -10, 20, -10, 40 });
		renderer.setZoomLimits(new double[] { -10, 20, -10, 40 });
		
		XYValueSeries sunSeries = new XYValueSeries("Sunshine hours");
		sunSeries.add(1, 35, 4.3);
		sunSeries.add(2, 35, 4.9);
		sunSeries.add(3, 35, 5.9);
		sunSeries.add(4, 35, 8.8);
		sunSeries.add(5, 35, 10.8);
		sunSeries.add(6, 35, 11.9);
		sunSeries.add(7, 35, 13.6);
		sunSeries.add(8, 35, 12.8);
		sunSeries.add(9, 35, 11.4);
		sunSeries.add(10, 35, 9.5);
		sunSeries.add(11, 35, 7.5);
		sunSeries.add(12, 35, 5.5);
		XYSeriesRenderer lightRenderer = new XYSeriesRenderer();
		lightRenderer.setColor(Color.YELLOW);
		
		waterSeries = new XYSeries("Water Temperature");
		for (int i = 0; i < mData.length; i++)
		{
			waterSeries.add(i + 1, mData[i]);
		}
		renderer.setBarSpacing(0.5);
		XYSeriesRenderer waterRenderer = new XYSeriesRenderer();
		waterRenderer.setColor(Color.argb(250, 0, 210, 250));
		
		XYMultipleSeriesDataset dataset = buildDataset(titles, x, values);
		dataset.addSeries(0, sunSeries);
		dataset.addSeries(0, waterSeries);
		renderer.addSeriesRenderer(0, lightRenderer);
		renderer.addSeriesRenderer(0, waterRenderer);
		waterRenderer.setDisplayChartValues(true);
		waterRenderer.setChartValuesTextSize(10);
		
		mChart = new CombinedXYChart(dataset, renderer, types);
		
	}
	
	/**
	 * Sets a few of the series renderer settings.
	 * 
	 * @param renderer the renderer to set the properties to
	 * @param title the chart title
	 * @param xTitle the title for the X axis
	 * @param yTitle the title for the Y axis
	 * @param xMin the minimum value on the X axis
	 * @param xMax the maximum value on the X axis
	 * @param yMin the minimum value on the Y axis
	 * @param yMax the maximum value on the Y axis
	 * @param axesColor the axes color
	 * @param labelsColor the labels color
	 */
	protected void setChartSettings(XYMultipleSeriesRenderer renderer, String title, String xTitle,
			String yTitle, double xMin, double xMax, double yMin, double yMax, int axesColor, int labelsColor)
	{
		renderer.setChartTitle(title);
		renderer.setXTitle(xTitle);
		renderer.setYTitle(yTitle);
		renderer.setXAxisMin(xMin);
		renderer.setXAxisMax(xMax);
		renderer.setYAxisMin(yMin);
		renderer.setYAxisMax(yMax);
		renderer.setAxesColor(axesColor);
		renderer.setLabelsColor(labelsColor);
	}
	
	/**
	 * @param inputData 更新的数据，实现动态增长
	 */
	public void updateData(double[] inputData)
	{
		mData = inputData;
		this.init(mContext, mColors, mStyles, inputData);
	}
}
